package com.pengtu.gsj.service.impl.yh;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.yh.DamageDiseaseDao;
import com.pengtu.gsj.entity.yh.DamageDisease;
import com.pengtu.gsj.service.yh.DamageDiseaseService;
import com.pengtu.gsj.utils.ConfigUtils;
import com.pengtu.gsj.utils.encode.JsonBinder;

@Transactional
@Service
public class DamageDiseaseServiceImpl implements DamageDiseaseService {
	
	@Autowired
	private DamageDiseaseDao damageDiseaseDao;

	@Override
	public void dockingRoadRepair() throws Exception {
        Date time = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(time);
        String lastTime = damageDiseaseDao.getMaxDate();
        String urlString = ConfigUtils.getConfig("repairAddress");
        // 如果从文件中读取的URL地址最后一个字符不是 '\'，则添加'\'    
        if (urlString.charAt(urlString.length() - 1) != '/') {    
        	urlString += "/";    
        } 
        urlString+="gsj/disease.action?";
        if(StringUtils.isNotBlank(lastTime)){
//            urlString += "beginTime="+URLEncoder.encode(lastTime,"utf-8");
            urlString += "beginTime="+URLEncoder.encode("2015-05-26 00:00:00","utf-8");
        }
        if(StringUtils.isNotBlank(currentTime)){
//            urlString += "&endTime="+URLEncoder.encode(currentTime,"utf-8");
            urlString += "&endTime="+URLEncoder.encode("2015-05-30 00:00:00","utf-8");
        }
        System.out.println("url:"+urlString);
        URL url;
        StringBuffer st=new StringBuffer();
        url = new URL(urlString);
        URLConnection urlData=url.openConnection();
        urlData.setConnectTimeout(30000);
        BufferedReader reader =new BufferedReader(new InputStreamReader(urlData.getInputStream()));
        String line=null;
        while((line=reader.readLine())!=null){
            st.append(line);
        }
        String sts=st.toString();
        JsonBinder binder=new JsonBinder(Inclusion.ALWAYS);
        List<DamageDisease> list = binder.fromJsonList(sts, new TypeReference<List<DamageDisease>>() {});
        if(list!=null){
            for(DamageDisease entity :list){
                damageDiseaseDao.saveOnly(entity);
            }
        }
	}

	@Override
	public Page<DamageDisease> getDamageDiseaseList(Page<DamageDisease> page,
			QueryFilter filters) {
		return damageDiseaseDao.findPage(page, filters);
	}

	@Override
	public DamageDisease getDamageDiseaseById(String id) {
		return damageDiseaseDao.get(id);
	}

	@Override
	public void saveDamageDisease(DamageDisease damageDisease) {
		damageDiseaseDao.save(damageDisease);
	}

}
